package com.naiterui.ehp.bs.ds.modules.sys.controller;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bs.ds.modules.sys.entity.Dict;
import com.naiterui.ehp.bs.ds.modules.sys.service.IDictService;
import com.naiterui.ehp.bs.ds.modules.sys.vo.DictVO;

import io.swagger.annotations.Api;

/**
 * 数据字典
 */
@Validated
@Api(tags = {SwaggerTag.DS + SwaggerTag.SYS})
@RestController
@RequestMapping("sys/dict")
public class DictController {

  @Autowired
  private IDictService dictService;

  /**
   * 字典列表
   *
   * @return
   */
  @SysLog("字典列表")
  @GetMapping("/list")
  @PreAuthorize("@p.check('sys:dict:list')")
  public List<Dict> list() {
    return this.dictService.queryList();
  }

  @GetMapping("/vue/list/{type}")
  public List<DictVO> vueList(@PathVariable("type") String type) {
    return this.dictService.queryVueList(type);
  }

  /**
   * 信息
   *
   * @return
   */
  @SysLog("字典信息")
  @GetMapping("/info/{id}")
  @PreAuthorize("@p.check('sys:dict:info')")
  public Dict info(@PathVariable("id") Long id) {
    return this.dictService.getById(id);
  }

  /**
   * 保存
   */
  @SysLog("保存字典")
  @PostMapping("/save")
  @PreAuthorize("@p.check('sys:dict:save')")
  public void save(@Valid @RequestBody Dict dict) {
    this.dictService.save(dict);
  }

  /**
   * 修改
   */
  @SysLog("修改字典")
  @PostMapping("/update")
  @PreAuthorize("@p.check('sys:dict:update')")
  public void update(@Valid @RequestBody Dict dict) {
    Preconditions.checkNotNull(dict.getId(), "字典ID不能为null");
    this.dictService.updateById(dict);
  }

  /**
   * 删除
   */
  @SysLog("删除字典")
  @PostMapping("/delete")
  @PreAuthorize("@p.check('sys:dict:delete')")
  public void delete(@RequestBody Long[] ids) {
    this.dictService.removeByIds(Arrays.asList(ids));
  }

}
